/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelLimparRua.java
 *
 * Created on 23/09/2011, 07:29:55
 */

package modules.enderecamento.gui.jpanel;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import modules.enderecamento.facade.WMSFacade;
import modules.enderecamento.models.dto.Enderecamento;
import modules.enderecamento.models.dto.Palete;
import modules.enderecamento.models.dto.Produto;
import modules.enderecamento.models.dto.Rua;
import modules.enderecamento.util.BoxPanel;

/**
 *
 * @author Clezio
 */
public class PanelLimparRua extends javax.swing.JPanel {

    private WMSFacade wmsFacade;
    private String ruaConsultadoGlobal;
    /** Creates new form PanelLimparRua */
    public PanelLimparRua() {
        initComponents();
        wmsFacade = new WMSFacade();
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(38);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(94);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(325);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(40);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfRua = new modules.enderecamento.gui.custom.WMSJTextField();
        lbCubagem = new modules.enderecamento.gui.custom.WMSLabel();
        btnConsultar = new modules.enderecamento.gui.custom.WMSJButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new modules.enderecamento.gui.custom.WMSJTable();
        btnLimpar = new modules.enderecamento.gui.custom.WMSJButton();

        setBackground(new java.awt.Color(238, 238, 238));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Remoção de Produtos da Rua");

        tfRua.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfRuaKeyPressed(evt);
            }
        });

        lbCubagem.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCubagem.setText("Rua:");

        btnConsultar.setMnemonic('C');
        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });
        btnConsultar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnConsultarKeyPressed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Palete", "Código", "Descrição", "Qtde"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        btnLimpar.setMnemonic('L');
        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 772, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(lbCubagem, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 544, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 586, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(117, 117, 117)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbCubagem, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfRuaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfRuaKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            if (tfRua.getText().length() > 0) {
                consultarProdutosRua();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfRua.setText("");
        }
}//GEN-LAST:event_tfRuaKeyPressed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        consultarProdutosRua();
}//GEN-LAST:event_btnConsultarActionPerformed

    private void btnConsultarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnConsultarKeyPressed

}//GEN-LAST:event_btnConsultarKeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        System.out.println(jTable1.getColumnModel().getColumn(0).getPreferredWidth());
        System.out.println(jTable1.getColumnModel().getColumn(1).getPreferredWidth());
        System.out.println(jTable1.getColumnModel().getColumn(2).getPreferredWidth());
        System.out.println(jTable1.getColumnModel().getColumn(3).getPreferredWidth());
}//GEN-LAST:event_jTable1MouseClicked

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed

        removerProdutosRua();
}//GEN-LAST:event_btnLimparActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private modules.enderecamento.gui.custom.WMSJButton btnConsultar;
    private modules.enderecamento.gui.custom.WMSJButton btnLimpar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private modules.enderecamento.gui.custom.WMSJTable jTable1;
    private modules.enderecamento.gui.custom.WMSLabel lbCubagem;
    private modules.enderecamento.gui.custom.WMSJTextField tfRua;
    // End of variables declaration//GEN-END:variables

    private void consultarProdutosRua() {
        if (tfRua.getText().length() > 0) {
            ruaConsultadoGlobal = tfRua.getText();
            List<Enderecamento> enderecamentos = wmsFacade.obterEnderecamento("from Enderecamento where palete.rua.nome = '"+tfRua.getText()+"'");
            if(enderecamentos != null){
                if(enderecamentos.isEmpty()){
                    Rua rua = wmsFacade.obterRuaByName(tfRua.getText());
                    if(rua == null){
                        BoxPanel.showMessageDialog(null, "Rua não encontrada.");
                    }else{
                        BoxPanel.showMessageDialog(null, "A Rua se encontra sem produtos.");
                    }
                    ruaConsultadoGlobal = null;
                }
                DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                modelo.setNumRows(0);

                for (int i = 0; i < enderecamentos.size(); i++) {
                    Palete palete = enderecamentos.get(i).getPalete();
                    Produto produto = enderecamentos.get(i).getProduto();
                    Integer qtde = enderecamentos.get(i).getQuantidade();

                    modelo.addRow(new Object[]{
                                palete.getId(),produto.getCodigo(), produto.getDescricao(), qtde
                            });
                }
            }
        }else{
            BoxPanel.showMessageDialog(null, "Informe a Rua");
        }
        
    }

    private void removerProdutosRua() {
        if(ruaConsultadoGlobal != null){
            if(BoxPanel.showConfirmDialog(null,"Deseja realmente remover os produtos endereçados nessa Rua?") == 0){
                if(new WMSFacade().removeProdutosRua(ruaConsultadoGlobal)){
                    DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                    modelo.setNumRows(0);
                    BoxPanel.showMessageDialog(null, "Operação realizada com sucesso.");
                }
            }
        }else{
            BoxPanel.showMessageDialog(null, "Efetue a consulta de uma Rua");
        }
    }

}
